package com.flycent.hqhs.ui;


import java.lang.ref.WeakReference;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.flycent.hqhs.App;
import com.flycent.hqhs.Constants;
import com.flycent.hqhs.R;
import com.flycent.hqhs.http.WebClient;
import com.flycent.hqhs.http.WebException;
import com.flycent.hqhs.util.StringUtil;

/**
 *  忘记密码
 * @author fbl
 */
public class ForgotPwdActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.userName_editText) EditText mUserNameEdit;
	@ViewInject(id = R.id.image_text_edit) EditText mVeifyTextEdit;
	@ViewInject(id = R.id.newPwdBtn,click = "butClick") Button mNewPwdBut;
	@ViewInject(id = R.id.forget_refreshImg,click = "butClick") ImageView mImageRefresh;
	@ViewInject(id = R.id.forget_verify_web) WebView mImageWeb;

	private String mUserVeifyImgId;
	public static ForgotPwdActivity instance = null;
	private Handler mHandler;
	
	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_forgot_pwd);
		FinalActivity.initInjectedView(this);
		instance = this;
		
		mHandler = new MyHandler(this);

		getTitleBar().setTitle(R.string.title_find_pwd);

		mUserVeifyImgId = getMyUUID();

		refreshWebView();

		setFocusChangeListen();
	}



	private void setFocusChangeListen() {
		mUserNameEdit.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View paramView, boolean hasFocus) {
				if( !hasFocus ){
					if(invalidateText(mUserNameEdit)){
						mUserNameEdit.setError(getString(R.string.info_please_input_user_no));
						return;
					}
					if( !(StringUtil.isPhoneNo(getText(mUserNameEdit)) 
							|| StringUtil.isEmail(getText(mUserNameEdit)) ) ){
						mUserNameEdit.setError(getString(R.string.txt_info_true_username_no));
						return;
					}
					//检测用户名是否存在
					doCheckUserNameExits();
				}
			}


		});
	}

	private void doCheckUserNameExits() {

		new Thread(){
			public void run() {
				try {
					WebClient.getInstance(ForgotPwdActivity.this).checkUsername(getText(mUserNameEdit));
					mHandler.sendEmptyMessage(0);
				} catch (WebException e) {
				}
			}
		}.start();


	}


	public void butClick(View v){

		if(v == mImageRefresh ){
			refreshWebView();
			mVeifyTextEdit.setText("");
		}
		if( v == mNewPwdBut ){
			doFindPassJudge();
		}
	}

	private void doFindPassJudge() {
		if( invalidateText(mUserNameEdit,R.string.info_please_input_user_no)
				|| invalidateText(mVeifyTextEdit,R.string.txt_info_verify_no)
				){
			return;
		}
		if( !(StringUtil.isPhoneNo(getText(mUserNameEdit)) 
				|| StringUtil.isEmail(getText(mUserNameEdit)) ) ){
			App.showShortToast(getString(R.string.txt_info_true_username_no));
			return;
		}
		doFindPwd();
	}



	private void refreshWebView() {
		String imgUri = Constants.IP+Constants.INFO.CAPTCHAIMAGE+mUserVeifyImgId;
		//直接用webview来显示图片，用image显示的话因为有缓存，不会刷新
		mImageWeb.loadUrl(imgUri);

	}

	private void doFindPwd() {
		showLoadingDialog("");

		new Thread(){
			public void run() {
				try {
					WebClient.getInstance(ForgotPwdActivity.this).passwordFind(getText(mUserNameEdit), mUserVeifyImgId, getText(mVeifyTextEdit));
					mHandler.sendEmptyMessage(1);
				} catch (Exception e) {
					e.printStackTrace();
					mHandler.sendEmptyMessage(2);
				}


			};
		}.start();

	}
	
	@SuppressLint("HandlerLeak") 
	private class MyHandler extends Handler{
		
		private WeakReference<Activity> mActivity;
		
		private MyHandler(Activity activity){
			mActivity = new WeakReference<Activity>(activity);
		}
		
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if(mActivity.get() != null ){
				switch (msg.what) {
				case 0:
					//输入的用户名不存在时提示用户
					mUserNameEdit.setError(getString(R.string.info_please_input_user_is_no_exist));
					break;
				case 1:
					//请求成功
					dismissLoadingDialog();
//					Intent intent = new Intent(ForgotPwdActivity.this,ResetPwdActivity.class);
					
					Intent intent = new Intent(ForgotPwdActivity.this,GetCodeActivity.class);
					intent.putExtra("userName", getText(mUserNameEdit));
					startActivity(intent);

					break;
				case 2:
					//请求失败
					dismissLoadingDialog();
					App.showShortToast(R.string.request_failure);
					break;

				default:
					break;
				}
			}
			
		}
		
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeCallbacksAndMessages(null);
	}

}
